﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EasyDLL.Tool
{
    public partial class ExecuteTimeMonitor : Form
    {
        private static Hashtable hashTable = new Hashtable();

       ListViewColumnSorter lvwColumnSorter  = new ListViewColumnSorter();

        public ExecuteTimeMonitor()
        {
            InitializeComponent();

            listView1.ListViewItemSorter = lvwColumnSorter;
        }

        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case NativeAPIWrapper.WM_COPYDATA:
                    string str = NativeAPIWrapper.Receive(ref m);
                    string[] msg = str.Split(',');
                    string name = msg[0];
                    double time = Convert.ToDouble(msg[1]);

                    UpdateHashTable(name, time);
                    UpdateListViewData();

                    break;
                default:
                    base.DefWndProc(ref m);
                    break;

            }
        }

        private void UpdateListViewData()
        {
            listView1.Items.Clear();

            foreach (DictionaryEntry entry in hashTable)
            {
                ListViewItem item = new ListViewItem(new string[]
                                                         {
                                                             entry.Key.ToString(),
                                                             entry.Value.ToString()
                                                         });
                listView1.Items.Add(item);
            }
        }

        private void UpdateHashTable(string name, double time)
        {
            if (hashTable.ContainsKey(name))
            {
                hashTable[name] = (Convert.ToDouble(hashTable[name]) + time) / 2;
            }
            else
            {
                hashTable[name] = time;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            hashTable.Clear();
            listView1.Items.Clear();
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // 重新设置此列的排序方法.
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // 设置排序列，默认为正向排序
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
            // 用新的排序方法对ListView排序
            this.listView1.Sort();           
        }
    }

    public class ListViewColumnSorter : IComparer
    {
        private int ColumnToSort;// 指定按照哪个列排序      
        private SortOrder OrderOfSort;// 指定排序的方式               
        private CaseInsensitiveComparer ObjectCompare;// 声明CaseInsensitiveComparer类对象，
        public ListViewColumnSorter()// 构造函数
        {
            ColumnToSort = 0;// 默认按第一列排序            
            OrderOfSort = SortOrder.None;// 排序方式为不排序            
            ObjectCompare = new CaseInsensitiveComparer();// 初始化CaseInsensitiveComparer类对象
        }
        // 重写IComparer接口.        
        // <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;
            ListViewItem listviewX, listviewY;
            // 将比较对象转换为ListViewItem对象
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;
            // 比较
            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == SortOrder.Ascending)
            {   // 因为是正序排序，所以直接返回结果
                return compareResult;
            }
            else if (OrderOfSort == SortOrder.Descending)
            {  // 如果是反序排序，所以要取负值再返回
                return (-compareResult);
            }
            else
            {
                // 如果相等返回0
                return 0;
            }
        }
        /// 获取或设置按照哪一列排序.        
        public int SortColumn
        {
            set
            {
                ColumnToSort = value;
            }
            get
            {
                return ColumnToSort;
            }
        }
        /// 获取或设置排序方式.    
        public SortOrder Order
        {
            set
            {
                OrderOfSort = value;
            }
            get
            {
                return OrderOfSort;
            }
        }
    }
}
